#include "stdafx.h"
#include "Column.h"

namespace ExcelLibrary
{
	CColumn::CColumn(Field* pField) : m_dwRefCount(0), m_pField(pField)
	{
		VariantInit(&m_Variant);

		AddRef();
	}

	CColumn::~CColumn()
	{
		m_pField->Release();

		VariantClear(&m_Variant);
	}

	HRESULT STDMETHODCALLTYPE CColumn::QueryInterface(REFIID riid, void __RPC_FAR *__RPC_FAR *ppvObject)
	{
		m_dwRefCount++;
		*ppvObject = static_cast<void*>(this);
		return S_OK;
	}
    
	ULONG STDMETHODCALLTYPE CColumn::AddRef(void)
	{
		m_dwRefCount++;
		return m_dwRefCount;
	}
    
	ULONG STDMETHODCALLTYPE CColumn::Release(void)
	{
		m_dwRefCount--;

		DWORD dwRefCount = m_dwRefCount;

		if (dwRefCount == 0)
		{
			delete this;
		}

		return dwRefCount;
	}

	bool CColumn::Initialize()
	{
		if (FAILED(m_pField->get_Value(&m_Variant)))
		{
			return false;
		}

		if (m_Variant.vt == VT_NULL)
		{
			return false;
		}

		return true;
	}

	VARTYPE CColumn::GetType()
	{
		return m_Variant.vt;
	}

	bool CColumn::IsNull()
	{
		if (m_Variant.vt != VT_NULL)
		{
			return false;
		}

		return true;
	}

	int CColumn::GetIntValue()
	{
		return m_Variant;
	}

	float CColumn::GetFloatValue()
	{
		return m_Variant;
	}

	const wchar_t* CColumn::GetStringValue()
	{
		return reinterpret_cast<const wchar_t*>(m_Variant.pbstrVal);
	}

	_variant_t CColumn::GetVariant()
	{
		return m_Variant;
	}
}